/*
 * @Description: 档案鉴定管理reducer
 * @Author: Rfan
 * @Date: 2022-07-29 16:18:02
 * @LastEditTime: 2022-07-29 19:22:05
 */

import { jsonClone } from '@/utils';
import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';
import {
  SET_BATCH_LIST,
  DELETE_BATCH_ITEM,
  SET_SELECTED_ITEM,
  ADD_BATCH_ITEM,
  EDIT_BATCH_ITEM
} from '../constants';

export const AppraisalMgtReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 设置档案鉴定批次列表
     * @param {Array} data 导出单列表
     */
    case SET_BATCH_LIST: {
      return {
        ...state,
        batchList: action.data
      };
    }

    /**
     * @description: 添加档案鉴定批次列表
     * @param {Array} data 导出单列表
     */
    case ADD_BATCH_ITEM: {
      return {
        ...state,
        batchList: insertItem(action.data, state.batchList)
      };
    }

    /**
     * @description: 添加档案鉴定批次列表
     * @param {Array} data 导出单列表
     */
    case EDIT_BATCH_ITEM: {
      const { id, data } = action;
      return {
        ...state,
        batchList: editItemById(id, data, state.batchList)
      };
    }

    /**
     * @description: 删除档案鉴定批次
     * @param {number[]} ids 删除的ID列表
     */
    case DELETE_BATCH_ITEM: {
      return {
        ...state,
        batchList: delItemByIdArray(action.ids, state.batchList)
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
